#include "authdatascop.h"
#include "ui_authdatascop.h"

#include <QPixmap>

AuthDataScop::AuthDataScop(QString id,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AuthDataScop)
{
    ui->setupUi(this);
    this->id = id;
    setWindowTitle("分配数据权限");
    setWindowIcon(QPixmap(":/image/res/stm.jpg"));

    connect(ui->treeWidget,SIGNAL(itemExpanded(QTreeWidgetItem*)),this,SLOT(itemExpanded(QTreeWidgetItem*)));
    connect(ui->treeWidget,SIGNAL(itemCollapsed(QTreeWidgetItem*)),this,SLOT(itemCollapsed(QTreeWidgetItem*)));
    connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));
    init();
}

AuthDataScop::~AuthDataScop()
{
    delete ui;
}

void AuthDataScop::init()
{
    QByteArray *bytes = roleManagerApi.getById(id);
    QJsonObject obj = QJsonDocument::fromJson(*bytes).object();
    QJsonObject data = obj.value("data").toObject();
    role = new Role(Role::toRole(data));
    if(obj.value("code") != 200){
        return;
    }else{
        ui->lineEdit_roleName->setText(role->roleName);
        ui->lineEdit_roleKey->setText(role->roleKey);
        ui->checkBox_fatherAndSon->setChecked(role->menuCheckStrictly == 1 ? 1 : 0);
    }

    QByteArray *bytes1 = roleManagerApi.deptTree(id);
    QJsonObject obj1 = QJsonDocument::fromJson(*bytes1).object();
    QJsonObject data1 = obj.value("data").toObject();
    role = new Role(Role::toRole(data1));
    if(obj.value("code") != 200){
        return;
    }else{
        QJsonArray depts = obj1.value("depts").toArray();
        treeNodeNum = depts.size();
        for(int i = 0;i< depts.size();i++){
            QJsonObject deptObj = depts.at(i).toObject();
            TreeSelect treeSelect = TreeSelect::toTreeSelect(deptObj);
            QTreeWidgetItem *menu = new QTreeWidgetItem(ui->treeWidget,
                    QStringList(deptObj.value("label").toString()));
            menu->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
            menu->setCheckState(0,Qt::Unchecked);
            QList<TreeSelect> list = treeSelect.children;
            menu->setData(1,0,treeSelect.id);
            setTreeMenu(&list,menu);
        }
        QJsonArray checkedKeys = obj1.value("checkedKeys").toArray();
        if(!checkedKeys.isEmpty()){
            ui->comboBox->setCurrentIndex(1);
            ui->treeWidget->expandAll();
            for(int i = 0;i< checkedKeys.size();i++){
                int count = ui->treeWidget->topLevelItemCount();
                for(int j = 0;j< count;j++){
                    QTreeWidgetItem *item = ui->treeWidget->topLevelItem(j);
                    if(checkedKeys.at(i).toInt() == ui->treeWidget->topLevelItem(j)->data(1,0)){
                        ui->treeWidget->topLevelItem(j)->setCheckState(0,Qt::Checked);
                    }
                    backShouDept(item,checkedKeys);
                }
            }
        }
    }
}

void AuthDataScop::setTreeMenu(QList<TreeSelect> *treeList, QTreeWidgetItem *menu)
{
    treeNodeNum +=  treeList->size();
    for(int i = 0;i< treeList->size();i++){
        QTreeWidgetItem *childmenu = new QTreeWidgetItem(menu,
                                                         QStringList(treeList->at(i).label));
        childmenu->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
        childmenu->setCheckState(0,Qt::Unchecked);
        QList<TreeSelect> l = treeList->at(i).children;
        childmenu->setData(1,0,treeList->at(i).id);
        setTreeMenu(&l,childmenu);
    }
}

void AuthDataScop::backShouDept(QTreeWidgetItem *item, QJsonArray data)
{
    int count = item->childCount();
    for(int i = 0;i< data.size();i++){
        for(int j = 0;j< count;j++){
            if(data.at(i).toInt() == item->child(j)->data(1,0)){
                item->child(j)->setCheckState(0,Qt::Checked);
            }else{
                backShouDept(item->child(j),data);
            }
        }
    }
}

void AuthDataScop::on_comboBox_currentIndexChanged(int index)
{
    if(index == 1){
        ui->widget_2->setEnabled(1);
        ui->treeWidget->setEnabled(1);
    }else{
        ui->widget_2->setEnabled(0);
        ui->treeWidget->setEnabled(0);
    }
}

void AuthDataScop::treeItemChanged(QTreeWidgetItem *item, int column)
{
    if(Qt::Checked == item->checkState(0)){
        int count = item->childCount();
        if(count > 0){
            for(int i = 0;i< count;i++){
                item->child(i)->setCheckState(0,Qt::Checked);
            }
        }else{
            utils.updateParentItem(item);
        }
    }else if(Qt::Unchecked == item->checkState(0)){
        int count = item->childCount();
        if(count > 0){
            for(int i = 0;i< count;i++){
                item->child(i)->setCheckState(0,Qt::Unchecked);
            }
        }else{
            utils.updateParentItem(item);
        }
    }
}

void AuthDataScop::itemExpanded(QTreeWidgetItem *item)
{
    int count = item->childCount();
    for(int i = 0;i< count;i++){
        QTreeWidgetItem *child = item->child(i);
        if(child->isExpanded()){
            itemExpanded(child);
        }
    }
}

void AuthDataScop::itemCollapsed(QTreeWidgetItem *item)
{
    int count = item->childCount();
    for(int i = 0;i< count;i++){
        if(item->child(i)->isExpanded()){
            itemCollapsed(item->child(i));
        }
    }
}


void AuthDataScop::on_pushButton_cancel_clicked()
{
    this->close();
}


void AuthDataScop::on_checkBox_expand_clicked()
{
    if(ui->checkBox_expand->isChecked()){
        ui->treeWidget->expandAll();
    }else{
        ui->treeWidget->collapseAll();
    }
}


void AuthDataScop::on_checkBox_fatherAndSon_clicked()
{
    fatherAndSonLink = ui->checkBox_fatherAndSon->isChecked();
    if(fatherAndSonLink){
        connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));
    }else{
        disconnect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));
    }
}


void AuthDataScop::on_checkBox_selectAll_clicked()
{
    int count = ui->treeWidget->topLevelItemCount();
    if(ui->checkBox_selectAll->isChecked()){
        for(int i = 0;i< count;i++){
            QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
            utils.selectAll(item);
        }
    }else{
        for(int i = 0;i< count;i++){
            QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
            utils.unSelectAll(item);
        }
    }
}


void AuthDataScop::on_btn_ok_clicked()
{
    int dataScop = ui->comboBox->currentIndex();
    dataScop++;
    role->dataScope = dataScop;
    QList<int> list;
    if(dataScop != 2){
        role->deptIds = list;
    }else{
        int count = ui->treeWidget->topLevelItemCount();
        for(int i = 0;i< count;i++){
            QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
            if(item->checkState(0) == Qt::Checked || item->checkState(0) == Qt::PartiallyChecked){
                list.append(item->data(1,0).toInt());
            }
            getTreeIds(list,item);
        }
        role->deptIds = list;
    }
    QByteArray *bytes = roleManagerApi.dataScope(*role);
    QJsonObject obj = QJsonDocument::fromJson(*bytes).object();
    if(obj.value("code") == 200){
        QMessageBox::warning(0,"提示",obj.value("msg").toString());
        close();
    }
}


void AuthDataScop::getTreeIds(QList<int> &list, QTreeWidgetItem *item)
{
    int count = item->childCount();
    for(int i = 0;i< count;i++){
        QTreeWidgetItem * child = item->child(i);
        if(child->checkState(0) == Qt::Checked || child->checkState(0) == Qt::PartiallyChecked){
            list.append(child->data(1,0).toInt());
        }
        getTreeIds(list,child);
    }
}
